package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.fault.entity.RequestTaskRecord;
import com.sduept.bigdata.fault.manager.RequestTaskManager;
import com.sduept.bigdata.fault.manager.RequestTaskManager.RequestTaskVO;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.core.Substation;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.fault.FaultTimingTaskService;
import com.sduept.utils.DateUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ViewScoped
@Named
public class RequestTaskConfigController extends AbstractController {
	private static final long serialVersionUID = 1658119088411457808L;

	@Autowired
	private RequestTaskManager rtm;
	@Autowired
	private CimManager cm;
	@Autowired
	private FaultTimingTaskService taskService;

	private List<RequestTaskVO> records = new ArrayList<RequestTaskVO>();
	private int showType;
	private Date faultTimeStart;
	private Date faultTimeEnd;

	
	@PostConstruct
	public void init() {
		initData();
	}

	public void initData() {
		faultTimeStart=new DateTime(new Date()).minusDays(7).toDate();
		faultTimeEnd=new Date();
		initView(0);
	}
	private void initView(Integer status){
		String start=DateUtil.getStringDate(faultTimeStart, DateUtil.pattern_ymd)+" 00:00:00";
		String end=DateUtil.getStringDate(faultTimeEnd, DateUtil.pattern_ymd)+" 23:59:59";
		Object[] objects=new Object[2];
		objects[0]=start;
		objects[1]=end;
		List<RequestTaskVO> rtn = rtm.listRecordsByStatusAndTaskID(status, objects);
		initVo(rtn);
	}
	private void initVo(List<RequestTaskVO> rs){
		records.clear();
		for(RequestTaskVO vo:rs){
			 String stationId = vo.getRecord().getStationId();
			 Substation substation = cm.getStationById(stationId);
			 if(null==substation){
				 continue;
			 }
			String stationName =substation.getName();
			vo.setStationName(stationName);
		}
		records.addAll(rs);
	}

	
	public void stopTask(RequestTaskVO current){
		RequestTaskRecord record = current.getRecord();
		if(record.isNeglectingConditions()){
			record.setTaskStatus(3);
		}else{
			record.setTaskStatus(2);
		}
		rtm.saveRecord(record);
		addSuccessMessage("已从待办任务中移除！");
	}
	public void startTask(RequestTaskVO current){
		RequestTaskRecord record = current.getRecord();
		if(3==record.getTaskStatus()){
			record.setNeglectingConditions(true);
		}
		record.setTaskStatus(0);
		rtm.saveRecord(record);
		List<RequestTaskRecord> list=new ArrayList<RequestTaskRecord>();
		list.add(record);
		taskService.doFaultTimingTask(list, true);
		addSuccessMessage("已加入到待办任务！");
	}
	public void showTasks() {
		if(5==showType){
			initView(null);
		}else{
			initView(showType);
		}
	}
	
	public String getFaultStatus(String type) {
		switch (type) {
		case "0":
			return "任务待执行";
		case "1":
			return "任务已执行，待返回";
		case "2":
			return "手动停止";
		case "3":
			return "任务过期";
		case "4":
			return "任务已完成";
		default:
			break;
		}
		return "";
	}

	/**
	 *  判断开始按钮是否可用：状态为0,2,3 可以显示可用
	 * @param status
	 * @return
	 */
	public boolean isAbleStart(int status) {
		switch (status) {
		case 0:
			return true;
		case 1:
			return false;
		case 2:
			return true;
		case 3:
			return true;
		case 4:
			return false;
		default:
			return false;
		}
	}
	/**
	 *  判断结束按钮是否可用：状态为1 可以显示可用
	 * @param status
	 * @return
	 */
	public boolean isAbleEnd(int status){
		switch (status) {
		case 0:
			return false;
		case 1:
			return true;
		case 2:
			return false;
		case 3:
			return false;
		case 4:
			return false;
		default:
			return false;
		}
	}
	

	public List<RequestTaskVO> getRecords() {
		return records;
	}

	public void setRecords(List<RequestTaskVO> records) {
		this.records = records;
	}

	public int getShowType() {
		return showType;
	}

	public void setShowType(int showType) {
		this.showType = showType;
	}

	public Date getFaultTimeStart() {
		return faultTimeStart;
	}

	public void setFaultTimeStart(Date faultTimeStart) {
		this.faultTimeStart = faultTimeStart;
	}

	public Date getFaultTimeEnd() {
		return faultTimeEnd;
	}

	public void setFaultTimeEnd(Date faultTimeEnd) {
		this.faultTimeEnd = faultTimeEnd;
	}

	

}
